//
//  PhotoAblumCell.m
//  testApp
//
//  Created by iMac on 2018/2/8.
//  Copyright © 2018年 iMac. All rights reserved.
//

#import "PhotoAblumCell.h"
#import <Photos/Photos.h>
#import "WDPhotoHandler.h"




CGFloat imageMargin = 2;
CGFloat largeMargin = 11;

@interface PhotoAblumCell()

@property (nonatomic, strong) UIImageView *previewImageView;
@property (nonatomic, strong) UILabel *ablumTitleLable;
@property (nonatomic, strong) UIImageView *arrowImageView;

@end

@implementation PhotoAblumCell


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self setupInit];
    }
    return self;
}

- (void)setModel:(AblumModel *)model {
    _model = model;
    self.ablumTitleLable.text = [NSString stringWithFormat:@"%@ (%ld)", model.ablum.localizedTitle, model.assetCount];
    // NSLog(@"%@", model.ablum.localizedTitle);
    NSLog(@"%f", self.contentView.bounds.size.height);
    CGSize size = CGSizeMake(model.cellHeight - 2 * imageMargin, model.cellHeight - 2 * imageMargin);
    [[WDPhotoHandler defaultHandler] thumbnalImage:model.firstAsset size:size completion:^(UIImage *image, NSDictionary *info) {
        if (!image) {
            self.previewImageView.backgroundColor = [UIColor redColor];
        }
        self.previewImageView.image = image;
    }];
//    [self layoutIfNeeded];
    [self.previewImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.contentView).offset(largeMargin);
        make.width.height.mas_equalTo(model.cellHeight - imageMargin * 2);
        make.centerY.mas_equalTo(self.contentView);
    }];
}

- (void)setupInit {
    [self.contentView addSubview:self.previewImageView];
    [self.contentView addSubview:self.ablumTitleLable];
    [self.contentView addSubview:self.arrowImageView];
    
    [self.previewImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.contentView).offset(largeMargin);
        make.width.height.mas_equalTo(self.bounds.size.height - imageMargin * 2);
        make.centerY.mas_equalTo(self.contentView);
    }];
    [self.ablumTitleLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.previewImageView.mas_right).offset(largeMargin);
        make.centerY.mas_equalTo(self.contentView);
    }];
    [self.arrowImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.contentView);
        make.right.mas_equalTo(self.contentView).offset(largeMargin);
    }];
    
}


#pragma mark: - lazy initialize

- (UIImageView *)previewImageView {
    if (!_previewImageView) {
        _previewImageView = [[UIImageView alloc] init];
    }
    return _previewImageView;
}
- (UILabel *)ablumTitleLable {
    if (!_ablumTitleLable) {
        _ablumTitleLable = [[UILabel alloc] init];
        _ablumTitleLable.textColor = [UIColor blueColor];
        _ablumTitleLable.numberOfLines = 1;
    }
    return _ablumTitleLable;
}
- (UIImageView *)arrowImageView {
    if (!_arrowImageView) {
        _arrowImageView = [[UIImageView alloc] init];
        [_arrowImageView sizeToFit];
    }
    return _arrowImageView;
}

@end
